<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class serviceAction extends CacheAction {
    public function  execute() {
        $this->uhash    = $this->params['uhash'];
        $this->view->layout = '';
        parent::execute();
        
    }
    
    public function actionDay() {
        load_help('DateTime');
        $hour   = getHourCount(3600*3,'-');
        asort($hour);
        //获取pv
        foreach ($hour as $k=>$v):
            $date   = date('G',$v['start']);
            if ($date==0) {
                $date   = 24;
            }
            $pv   = DB_ACCESS::getPv($this->uhash, $v['start'], $v['end']);
            $ip   = DB_IP::getIp($this->uhash, $v['start'], $v['end']);
            $dataset[]    = array('日期'=>$date,'浏览量'=>$ip,'独立访客'=>$pv);
            //$dataset[]    = array('pv'=>"$date",'ip'=> "$ip",'pv'=>"$pv");
        endforeach;
        
        $config = array('yTitle'=>'数量','categoryField'=>'日期','yField'=>'浏览量,独立访客');
        //$dataset[]    = array('日期'=>3,'浏览量'=>20,'独立访客'=>10);
        $init   = json_encode(array('config' =>$config,'dataset' =>$dataset));
        //print_r($init);
        $this->set('init', $init);
        $this->render();
    }
    
    public function actionCountDay() {
        $uhash  = $this->params['uhash'];
        if(!$uhash) {
            exit;
        }
        $key_ip    = 'count_day_'.$uhash.'_ip';
        $key_pv    = 'count_day_'.$uhash.'_pv';
        $key_y1    = 'count_day_'.$uhash.'_y1';
        $cache  = MemcacheAction::getCache();
        $ip    =    $cache->get($key_ip);
        $pv    =    $cache->get($key_pv);
        $y1    =    $cache->get($key_y1);
        if(!$ip) {
            load_help('DateTime');
            $hour   = getHourCount(3600*11,'-', 10);
            asort($hour);
            //获取pv
            foreach ($hour as $k=>$v):
                $date   = date('G',$v['start']);
                if ($date==0) {
                    $date   = 24;
                }
                $y1[]   = $date;
                $pv[]   = DB_ACCESS::getPv($uhash, $v['start'], $v['end']);
                $ip[]   = DB_IP::getIp($uhash, $v['start'], $v['end']);
                //$dataset[]    = array('pv'=>"$date",'ip'=> "$ip",'pv'=>"$pv");
            endforeach;
            $cache->set($key_ip, $ip, 3600);
            $cache->set($key_pv, $pv, 3600);
            $cache->set($key_y1, $y1, 3600);
        }
        $this->set('y1', $y1);
        $this->set('pv', $pv);
        $this->set('ip', $ip);
        $this->render();
    }
    
    public function actionCountMonth() {
        $uhash  = $this->params['uhash'];
        if(!$uhash) {
            exit;
        }
        $key_ip    = 'count_month_'.$uhash.'_ip';
        $key_pv    = 'count_month_'.$uhash.'_pv';
        $key_y1    = 'count_month_'.$uhash.'_y1';
        $cache  = MemcacheAction::getCache();
        $ip    =    $cache->get($key_ip);
        $pv    =    $cache->get($key_pv);
        $y1    =    $cache->get($key_y1);
        if(!$ip) {
            load_help('DateTime');
            $id = $this->params['id'];
            $time_stamp = 3600*24 *10;
            $hour   = getMonthCount($time_stamp, '-', 10);
            asort($hour);
            //获取pv
            foreach ($hour as $k=>$v):
                $y1[]   =  date('d',$v['start']);
                $pv[]   = DB_ACCESS::getPv($uhash, $v['start'], $v['end']);
                $ip[]   = DB_IP::getIp($uhash, $v['start'], $v['end']);
                //echo date('Y-m-d H:i:s',$v['start']).'--'.date('Y-m-d H:i:s',$v['end']).'<br/>';
            endforeach;
            $cache->set($key_ip, $ip, 3600);
            $cache->set($key_pv, $pv, 3600);
            $cache->set($key_y1, $y1, 3600);
        }
        $this->set('ip',  $ip);
        $this->set('pv',  $pv);
        $this->set('y1',  $y1);
        $this->render();
    }

    /**
     * 短网址信息
     */
    public function actionDetail(){
        $uhash  = $this->params['uhash'];
        if(empty($uhash)){
            exit;
        }
        
        $rs = DAO_Finder::findOne(DB_URL, array('uhash'=> $uhash));
        if($rs) {
            $title  = $rs->getTitle();
            if($title =='no'){
                $title  = getUrlTitle($rs->getUrl());
                $rs->setTitle($title);
                $rs->save();
            }
            $data   = $rs->getArray();
            $days   = round((time() - $data['created']) / (3600*24),0);
            $data['created']    = date('Y-m-d H:i:s', $data['created']);
            $data['days']       = $days;
            echo json_encode(array('code'=>'ok', 'data'=>$data));
        }else{
            echo json_encode(array('code' => 'error'));
        }
    }

    public function actionCount(){
        $arr    = array();
        //今日统计
        $log_time   = date('Ymd');
        $uhash  = $this->params['uhash'];
        $where  = "`uhash` = $uhash and log_time=$log_time";
        $rs = DAO_Finder::daoOne(DB_COUNT_IP, array('uhash'=>$uhash,'log_time'=>$log_time));
        $arr['day_ip'] = getCountIp($rs);
        $rs = DAO_Finder::daoOne(DB_COUNT_PV, array('uhash'=>$uhash,'log_time'=>$log_time));
        $arr['day_pv']  = getCountPv($rs);
        //昨日统计
        $log_time   = date('Ymd',(time()-3600*24));
        $where  = "`uhash` = $uhash and log_time=$log_time";
        $rs = DAO_Finder::daoOne(DB_COUNT_IP, array('uhash'=>$uhash,'log_time'=>$log_time));
        $arr['yes_ip']  = getCountIp($rs);
        $rs = DAO_Finder::daoOne(DB_COUNT_PV, array('uhash'=>$uhash,'log_time'=>$log_time));
        $arr['yes_pv']  = getCountPv($rs);


        //最高
        $max_pv  = DAO_Finder::findOne(DB_COUNT_PV, array('uhash' =>$uhash), 'pv,log_time', 'pv desc');
        $max_ip  = DAO_Finder::findOne(DB_COUNT_IP, array('uhash' =>$uhash), 'ip,log_time', 'ip desc');
        
        if(!$max_ip) {
            $max_ip = new DB_COUNT_IP();
            $max_ip->setLogTime(date("Ymd"));
            $max_ip->setIp(0);
        }

        if(!$max_pv) {
            $max_pv = new DB_COUNT_IP();
            $max_pv->setLogTime(date("Ymd"));
            $max_pv->setPv(0);
        }
        
        $arr['max_pv']  = $max_pv->getArray();
        $arr['max_pv_date'] = substr($arr['max_pv']['log_time'], 0,4).'-'.substr($arr['max_pv']['log_time'], 4,2).'-'.substr($arr['max_pv']['log_time'], 6);
        $arr['max_pv'] = $arr['max_pv']['pv'];

        $arr['max_ip']  = $max_ip->getArray();
        $arr['max_ip_date'] = substr($arr['max_ip']['log_time'], 0,4).'-'.substr($arr['max_ip']['log_time'], 4,2).'-'.substr($arr['max_ip']['log_time'], 6);
        $arr['max_ip'] = $arr['max_ip']['ip'];

        echo json_encode(array('code'=>'ok','data'=>$arr));
    }
}
?>
